package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import net.wanho.R.R;
import net.wanho.dto.LoginDTO;
import net.wanho.service.CaptchaService;
import net.wanho.service.LoginLogService;
import net.wanho.util.CurrentUtil;
import net.wanho.vo.UserVo;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 这个类是：
 *
 * @author: MENG HU WANG
 * @date: 2025/1/12 16:04
 * @version: 1.0
 */

@RestController
@RequiredArgsConstructor
public class LoginController {
    private final CaptchaService captchaService;
    private final LoginLogService loginService;
    private final RedisTemplate redisTemplate;

    // 获取验证码
    @GetMapping("/captchaImage")
    public R captchaImage() {
        return R.ok(captchaService.getCaptcha());
    }

    // 获取当前用户信息
    @GetMapping("curr-user")
    public R currUser() {
        UserVo userVo = captchaService.selectByUserId();

//        token = token.replace("Bearer ", "");
//        JWT parseToken = shiroJwtUtil.parseToken(token);
//        String userCode = (String) parseToken.getPayload("userCode");
//        UserInfo userInfo = userService.findByUserCode(userCode);
//        return AjaxResult.ok("success", userInfo);
        return R.ok(userVo);
    }


    // 登录
    @PostMapping("/login")
    public R login(@RequestBody LoginDTO loginDTO) {
        String token = loginService.login(loginDTO);
        return R.ok("登陆成功", token);
    }

    // 退出
    @DeleteMapping("/logout")
    public R logout() {
        CurrentUtil.removeUserId();
        redisTemplate.delete("userId");
        return R.ok("退出成功");
    }
}
